<?php

/*
 * Following code will list all the offers
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// building the sql-statement
$statement = "SELECT * FROM User ";

if(isset($_GET["UserID"]))
{
	$statement .= "WHERE UserID = " .$_GET["UserID"];
}
else if(isset($_GET["Username"]) && isset($_GET["Password"]))
{
	$statement .= "WHERE Username = '" .$_GET["Username"] . "' AND Password = '" .$_GET["Password"] . "'";
}
else if(isset($_GET["Username"]))
{
	$statement .= "WHERE Username = '" .$_GET["Username"] . "'";
}

// get all offers from offers table
$result = mysql_query($statement) or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // offers node
    $response["users"] = array();
    
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $user = array();
        $user["UserID"] = $row["UserID"];
        $user["Username"] = utf8_encode($row["Username"]);
        $user["Password"] = $row["Password"];
        $user["Firstname"] = utf8_encode($row["Firstname"]);
        $user["Lastname"] = utf8_encode($row["Lastname"]);
        $user["Email"] = utf8_encode($row["Email"]);

        // push single user into final response array
        array_push($response["users"], $user);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no offers found
    $response["success"] = 0;
    $response["message"] = "No Users found";

    // echo no users JSON
    echo json_encode($response);
}
?>
